package com.qire.manhua.activity;

import android.os.Bundle;
import android.view.View;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.FileManager;
import com.qire.common.utils.ThemeUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.SystemSetupController;
import com.qire.manhua.databinding.ActivitySystemSetupBinding;
import com.qire.manhua.model.viewModel.ISystemSetupViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.SystemSetupPage)
public class SystemSetupActivity extends BaseActivity<ActivitySystemSetupBinding> {

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<ISystemSetupViewModel> viewModel;

    @Autowired(injector = EventControllerInjector.class)
    SystemSetupController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_system_setup;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.setStatusBarColor(this, R.color.white);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        controller.onAddition(viewModel);

        viewModel.<Boolean>observe("enablePreferences", this, (enable) ->
                viewModel.<CharSequence>set("enablePreferencesTips", enable ? "关闭后，将无法根据您的阅读喜好推荐漫画" : "开启后，将根据您的阅读偏好推荐漫画")
        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshUI();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.removeObservers("enablePreferences", this);
    }

    private void refreshUI() {

        viewModel.set("autoPay", UserEntity.self.isAutoPay());
        viewModel.set("clearCacheClickable", true);
//        viewModel.set("cacheCapacityHint", GlideCacheUtil.getInstance().getCacheSize(activity));
        viewModel.set("cacheCapacityHint", StringUtil.formatStorageSize(FileManager.countGlideCacheSize())); // "0MB"
        viewModel.set("accountsAndSecurityHint", StringUtil.defValue(UserEntity.self.getMobile(), "未绑定手机号"));
        viewModel.set("teensModeHint", TeensModeManage.teensModeEnable() ? "已开启" : "未开启");
        viewModel.set("privacyProtocolRedDotVisible", DeviceConfigInfo.checkUpdatePrivacyProtocolVer() ? View.VISIBLE : View.GONE);
        viewModel.set("serviceProtocolRedDotVisible", DeviceConfigInfo.checkUpdateServiceProtocolVer() ? View.VISIBLE : View.GONE);
        viewModel.set("enablePreferences", UserEntity.self.getEnablePreferences());
        viewModel.set("version", "版本:" + DeviceConfigInfo.info().versionName);
        viewModel.set("loginOrLogoutBtnText", UserEntity.self.isLogin() ? "退出登录" : "立即登录");

    }

}
